package com.lanqiao.companymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lanqiao.companymanage.entity.Dep;
import com.lanqiao.companymanage.entity.User;
import com.lanqiao.companymanage.service.DepartmentMessService;
import com.lanqiao.companymanage.service.OperatingMessService;
import com.lanqiao.companymanage.service.StaffMessService;
import com.lanqiao.companymanage.utils.ExcelUtils;
import com.lanqiao.companymanage.utils.MD5Utils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 文件操作
 *
 * @author 2021/12/21  22:44
 */

@RestController
@RequestMapping("/File")
public class FileController {

    /**
     * 部门信息管理
     */
    @Autowired
    private StaffMessService staffMessService;

    /**
     * 职工信息管理
     */
    @Autowired
    private DepartmentMessService departmentMessService;

    /**
     * 经营信息
     */
    @Autowired
    private OperatingMessService operatingMessService;

    /**
     * 上传文件
     *
     * @return
     */
    @SneakyThrows
    @RequestMapping("/upLoadExcel")
    public Map<String, Object> upLoadExcel(@RequestParam("file") MultipartFile file) {
        //获取文件名
        String fileName = file.getOriginalFilename();
        //后去后缀名
        String suffix = fileName.substring(fileName.lastIndexOf('.'));
        //创建新的文件
        String newFileName = System.currentTimeMillis() + suffix;
        //(获取当前项目的绝对路径)文件临时存储位置
        File tempFile = new File(new File("src/main/resources", newFileName).getAbsolutePath());
        //下载临时文件到本地
        file.transferTo(tempFile);
        //获取Excel返回值对象
        Map<String, Object> map = ExcelUtils.ReadExcelToShow(tempFile);
        //获取 员工数据
        List<Object[]> list = (List<Object[]>) map.get("data");
        //遍历添加
        for (Object[] objects : list) {
            User user = new User(
                    null, String.valueOf(objects[1]),
                    MD5Utils.passWordToEncryption(objects[1] + "6174"),
                    Integer.parseInt(String.valueOf(objects[0])),
                    String.valueOf(objects[3]),
                    new SimpleDateFormat("yyyy-MM-dd").parse(String.valueOf(objects[4])));
            departmentMessService.save(user);
            //获取部门
            String dept = (String) objects[2];
            Dep dep = staffMessService.list(new QueryWrapper<Dep>().eq("name", dept)).get(0);
            staffMessService.update(new UpdateWrapper<Dep>()
                    .set("count", dep.getCount() + 1)
                    .eq("name", dept));
            System.out.println(user.getId());
            System.out.println(dep.getId());
        }
        return map;
    }

    /**
     * 下载文件到本地
     * @return
     */
    @RequestMapping("/downLoadExcel")
    public Map<String, Integer> downLoadExcel() {
        Map<String, Integer> map = ExcelUtils.DownLoadExcelToLocal(operatingMessService.list());
        System.out.println(map);
        return map;
    }
}